double dist(double h, double p) {
    double total = h;
    while (h * p >= TOL) {
        h *= p;
        total += 2 * h; 
    }
    return total;
}